package com.example.game;

import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.example.game.GamePage.databaseHelper;
import static com.example.game.GamePage.player;

public class setToolFragment extends Fragment implements AdapterView.OnItemClickListener{

    private ImageButton mLeftMenu;
    private ListView listView;
    private SimpleAdapter adapter;
    private List<Map<String,Object>>dataList;
    private setToolFragment ToolFragment;
    private setVideoFragment videoFragment;
    private setArticleFragment articleFragment;

    android.app.FragmentManager fm = getFragmentManager();
    List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>(); //存储数据的数组列表
    int CONTENT_COUNT = 3;
    int[] image = {R.drawable.tool_1, R.drawable.tool_2,R.drawable.tool_3,R.drawable.tool_4};
    String[][] content = new String[4][CONTENT_COUNT+1];
    final int REFRESH = 0;

    boolean tech_is_finished = false;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        Log.d("TAG_setToolFragment", "onCreateView: ");
        if(player.building_list[0][9]>0){
            tech_is_finished = true;
        }
        if(tech_is_finished){
            CONTENT_COUNT = 4;
        }


        View view = inflater.inflate(R.layout.tool_fragment_layout, container, false);

        //写死的数据，用于测试

        setContent();


//        for(int i=0; i<CONTENT_COUNT; i++)
//        {
//            Map<String, Object> map = new HashMap<String, Object>();
//            map.put("image", image[i]);
//            Log.d("set_image_in_create", "onCreateView: " + image[i]);
//            map.put("Name", content[0][i]);
//            map.put("content",content[1][i]);
//            listitem.add(map);
//        }

        //创建适配器
        // 第一个参数是上下文对象
        // 第二个是listitem
        // 第三个是指定每个列表项的布局文件
        // 第四个是指定Map对象中定义的两个键（这里通过字符串数组来指定）
        // 第五个是用于指定在布局文件中定义的id（也是用数组来指定）
        adapter = new SimpleAdapter(getActivity()
                , listitem
                , R.layout.tool_fragment_item
                , new String[]{"image","Name","content"}
                , new int[]{R.id.Tool_Image_View, R.id.Tool_Name,R.id.Tool_content});

        ListView listView = (ListView)view.findViewById(R.id.Tool_ListView);
        listView.setOnItemClickListener(this);
        listView.setAdapter(adapter);

        refresh_listview();



        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String TAG = "onitemclick";
        String myTag = "这是在设置Tool里面的点击监听";
        //获得父activity的fragmentManager
        GamePage act = (GamePage) getActivity();
        FragmentTransaction transaction = act.fm.beginTransaction();
//        String text= listView.getItemAtPosition(position)+"";
//        Toast.makeText(getActivity(), "position="+position+"text="+text,
//                Toast.LENGTH_SHORT).show();
        switch (position)
        {
            case 0:
                if(player.cash >= player.production*20) {
                    player.building_list[0][10] += 1;
                    player.cash -= (player.production*20);
                }
                Log.d(TAG, "onItemClick: case 0");
                setContent();
                refresh_listview();
                break;
            case 1:
                //选了看文章
                databaseHelper.saveGame();
                Log.d(myTag, "onClick: checkpoint1");
                articleFragment = new setArticleFragment();
                transaction.replace(R.id.Fragment, articleFragment);
                transaction.commit();
                Log.d(myTag, "onClick: checkpoint2");
                break;
            case 2:
                //选了看视频
                databaseHelper.saveGame();
                Log.d(myTag, "onClick: checkpoint1");
                videoFragment = new setVideoFragment();
                transaction.replace(R.id.Fragment, videoFragment);
                transaction.commit();
                Log.d(myTag, "onClick: checkpoint2");
                break;
            case 3:
//                if(player.cash >= 10000000000000L){
                if(player.cash >= 10000000000000L || player.building_list[0][14] == 1){
                    if(player.building_list[0][14] == 0) {
                        player.cash -= 10000000000000L;
                        player.building_list[0][14] = 1;
                    }
                    databaseHelper.saveGame();
                    Log.d(myTag, "onClick:最终真相 checkpoint1");
                    FinalPageFragment finalPageFragment = new FinalPageFragment();
                    transaction.replace(R.id.Fragment, finalPageFragment);
                    transaction.commit();
                    Log.d(myTag, "onClick: 最终真相checkpoint2");
                }
                break;
        }
    }
    public void setContent(){           //优化的时候可以区分一下初始化的时候，有些content项就不会变动了，就不用重新赋值了
        String TAG = "setContent";
        for(int i = 0; i < CONTENT_COUNT ; i ++){
            content[0][i] = player.tool_name[i];
            content[1][i] = player.tool_background[i];
        }
        content[1][0] = "价格：" + player.convertLongToString((long)(player.production*20)) + " / 当前营销号质量：" + (long)(player.building_list[0][10] + 1) +"\n" + " 可以增加每次点击产生的营销号数量！";
        Log.d(TAG, "setContent: finished");
    }
    public void refresh_listview(){
        listitem.clear();

        for(int i=0; i<CONTENT_COUNT; i++){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", image[i]);
            Log.d("set_image", "refresh_listview: " + this.image[i]);
            map.put("Name", content[0][i]);
            map.put("content",content[1][i]);
            listitem.add(map);
        }
        adapter.notifyDataSetChanged();
    }
}

